package com.sunbim.rsrv.mapper;

import com.sunbim.rsrv.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuhui
 * @since 2021-12-30
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("select count(1) from user where username=#{username}")
    boolean usernameExist(String username);

    @Select("select count(1) from user where id=#{id}")
    boolean idExist(int id);

    @Select("select password from user where username=#{username}")
    String getPasswordByUsername(String username);

    @Select("select id,nickname,user_role,region,project_name,project_desc,project_geo,create_time,update_time from user where username=#{username}")
    HashMap<String, Object> getUserByUsername(String username);

    @Select("select id,username,nickname,user_role,region,region_level,project_name,project_desc,project_geo,create_time,update_time from user where id=#{id}")
    HashMap<String, Object> getUserById(Object id);

    @Select("select user_role from user where id=#{id}")
    List<String> getRoleById(Object id);

    //SELECT count(0) FROM user WHERE (user_role <> 'admin');
    @Select("select u.id,u.nickname,u.user_role,u.region,u.project_name,u.project_desc,u.project_geo,u.create_time,u.update_time,s.storage_url as icon_url " +
            "from user u,storage s " +
            "where (u.user_role <> 'admin') and u.id = s.user_id")
    List<Map<String, Object>> getAllExcludeAdmin();

    @Select("select count(1) from user where username=#{username} and id <>#{id}")
    boolean usernameExistExcludeOwner(Integer id, String username);
}
